package com.springboot.service.blog;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.StrUtil;
import com.springboot.dao.BlogCommentDao;
import com.springboot.entity.BlogCommentEntity;
import com.springboot.entity.SystemMenuEntity;
import com.springboot.returndata.vo.RoleResourceVo;
import com.springboot.returndata.vo.SystemMenuVo;
import com.springboot.returndata.vo.blog.BlogCommentVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.util.common.TreeVoUtil;
import com.springboot.util.http.ResultResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 文章评论 服务类
 * </p>
 *
 * @author zyj
 * @since 2023-05-04
 */
@Service
public class BlogCommentService extends ServiceImpl<BlogCommentDao, BlogCommentEntity> {

    @Resource
    private BlogCommentDao blogCommentDao;

    /**
     * 方法描述 根据文章id获取评论
     * @since: 1.0
     * @param articleId
     * @return: Tree
     * @author: zyj
     * @date: 2023/5/25 19:22
     */
    public List<BlogCommentVo> getCommentById(Integer articleId){

        /**获取评论*/
        List<BlogCommentVo> blogComments = blogCommentDao.findByArticleId(articleId);
        if( blogComments==null || blogComments.isEmpty()){
            return ListUtil.empty();
        }

        /**转为树*/
        //转化为树列表
        for (BlogCommentVo blogCommentVo : blogComments) {
            Integer parentCommentId = blogCommentVo.getParentCommentId();
            blogCommentVo.setId(String.valueOf(blogCommentVo.getCommentId()));
            blogCommentVo.setParentId(parentCommentId==null ?"1":String.valueOf(parentCommentId));
        }
        List<BlogCommentVo> treeVoCommon = TreeVoUtil.getListTreeVoCommon(blogComments, "0", TreeVoUtil.key_replyBody);
        return treeVoCommon;
    }

}
